import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:flutter_module/beans/element_bean.dart';
import 'package:flutter_module/map/ni_types.dart';
import 'package:flutter_module/viewModels/drawer_manager_viewmodel.dart';
import 'package:flutter_module/views/drawer_pages/base_page.dart';
import 'package:flutter_module/views/mapFragmentWidgets/map_title_bar.dart';
import 'package:provider/src/provider.dart';

class ElementListPage extends BasePage {
  DrawerManagerViewModel? _read;

  @override
  MapTitleBarAdapter createMapTitleBarAdapter(BuildContext context) {
    _read ??= context.read<DrawerManagerViewModel>();
    return MapTitleBarAdapter(
      leading: BackButton(
        color: Colors.black,
        onPressed: () {
          _read!.backPage();
        },
      ),
    );
  }

  @override
  List<Widget> createWidgets(BuildContext context) {
    _read ??= context.read<DrawerManagerViewModel>();
    List<Widget> widgets = [];
    if (_read!.dataList != null) {
      for (var item in _read!.dataList!) {
        widgets.add(_createItem(item));
      }
    }

    return widgets;
  }

  Widget _createItem(ElementBean item) {
    return Card(
      color: Colors.blue,
      shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(8.0)),
      elevation: 8,
      child: ListTile(
        title: Text(item.displayText),
        trailing: const Icon(Icons.navigate_next_outlined),
        onTap: () {
          _read!.showElementEditPage(item);
        },
      ),
    );
  }

  @override
  void dispose() {}

  @override
  void update() {
    removeAllItem();
    if (_read?.dataList != null) {
      for (var item in _read!.dataList!) {
        addItem(_createItem(item));
      }
    }
  }
}
